define(window.backboneCommon.getViewDefine("finance.transferIn"), function ($,require,Collection,tpl) {
    var defaultsOptions={
        submitCallback:null
    };
    var pagePrivate = {
        getDefaults :function () {
            return defaultsOptions
        },
        getOptions : function (options) {
            options=$.ExtCommon.isCheckEmpty(options)?{}:options;
            return $.ExtCommon.initMergeJson(this.getDefaults(),options);
        }
    };
    var backboneView= Backbone.View.extend({
        events: {
            "change #txt_amountIn":"changeAmount",
            "click #btn_submitTransferIn": "saveSubmit"
        },
        initialize: function () {
            this.listenTo(this.model, 'save:AgentTransferIn', this.saveInfoCallback);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(options){
            this.elementModel={
                modal:$('#modal_detailsTransferIn'),
                form:$('#form_detailsTransferIn'),
                amount:$("#txt_amountIn"),
                actualAmount:$("#txt_actualAmountIn")
            };
            this.extObject={};
            this.pageData={initPage:false};
            this.pageOptions=$.ExtCommon.initMergeJson(pagePrivate.getOptions(),options);
            this.elementModel.form.ExtParsley();
        },
        initPage:function (info) {
            var self=this;
            self.extObject.formValue=self.elementModel.form.ExtFormValue({isRunBind:false,
                initCallback:function(dataJson) {
                    self.pageData.transferInInfo = dataJson;
                    self.elementModel.amount.attr("data-parsley-max-greater",dataJson.agentSurplusAmount);
                    $("#sp_agentSurplusAmount,#txt_amountIn,#txt_actualAmountIn").ExtNumeric("refresh");
                    self.elementModel.form.ExtParsley("reset");
                    self._showModal();
                }
            });
            self.extObject.formValue.cleanValue();
            self.extObject.formValue.bindValueData(info);
        },
        changeAmount:function (e) {
            var value=$(e.currentTarget).val();
            var actualAmount=$.ExtCommon.isCheckEmpty(value)?"":parseInt(value.removesplit())*(100-parseFloat(this.pageData.transferInInfo.serviceCost))/100;
            this.elementModel.actualAmount.val(actualAmount).ExtNumeric("refresh");
        },
        _checkForm:function () {
            return this.elementModel.form.ExtParsley("validate");
        },
        saveSubmit:function () {
            if(this._checkForm()){
                var ajaxData = this.elementModel.form.ExtFormValue("getValues");
                var infoOriginal = this.elementModel.form.ExtFormValue("getOriginalData");
                ajaxData.id=infoOriginal.id;
                this._hideModal();
                this.collection.saveAgentTransferIn(ajaxData);
            }
        },
        saveInfoCallback:function () {
            var result=this.model.get("dataSaveAgentTransferIn");
            if (result.res.success) {
                $.ExtNotify.success("保存成功!");
                if(this.pageOptions.submitCallback){this.pageOptions.submitCallback()}
            }else{
                this._showModal();
                $.ExtNotify.error(result.res.msg);
            }
        },
        _showModal:function(){
            this.elementModel.modal.modal("show");
            $.ExtAssist.modal.computeTop(this.elementModel.modal);
        },
        _hideModal: function () {
            this.elementModel.modal.modal("hide");
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({ el: options.pElement,collection:this.collection,model:this.model});
        this.view.initMain(options);
    };
    viewModel.prototype={
        init:function (data) {
            this.view.initPage(data);
        }
    };
    return viewModel;
});